package com.simple.editor;

import com.simple.editor.action.EditorUIAction;
import com.simple.editor.action.impl.*;
import com.simple.editor.ui.*;
import com.simple.editor.vcs.VcsClientRegistry;

import javax.swing.*;
import java.awt.*;


public class Main {

    private ApplicationContext ctx;

    public static void main(final String[] args) {
        new Main().run();
    }

    void run() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                initContext();

                final JFrame frame = new JFrame("Simple Editor");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                frame.add(ctx.getComponent(MainPane.class));
                frame.setJMenuBar(ctx.getComponent(MenuPane.class));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    private void initContext() {
        ctx = new ApplicationContextImpl();
        ctx.addComponent(ctx);
        ctx.addComponent(ActionManagerImpl.class);
        ctx.addComponent(VcsClientRegistry.class);

        ctx.addComponent(MainPane.class);
        ctx.addComponent(MenuPane.class);

        ctx.addComponent(ChangesPane.class);
        ctx.addComponent(FileTreePane.class);
        ctx.addComponent(TabbedEditorPane.class);
        ctx.addComponent(InfoPane.class);


        ctx.addComponent(EditorUIAction.Type.OPEN_PROJECT, OpenProjectEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.OPEN, OpenEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.CLOSE, CloseEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.MODIFY, ModifyEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.SAVE, SaveEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.DELETE, DeleteEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.REVERT, RevertEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.COMMIT, CommitEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.UPDATE, UpdateEditorUIAction.class);
        ctx.addComponent(EditorUIAction.Type.REFRESH, RefreshEditorUIAction.class);
    }
}